package com.weijing.pretty_app.util

import android.content.Context
import android.media.AudioManager

object VolumeManager {

    private const val KEY_VOLUME = "volume"
    private const val STREAM_TYPE = AudioManager.STREAM_MUSIC

    fun getMaxVolume(context: Context): Int {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return audioManager.getStreamMaxVolume(STREAM_TYPE)
    }

    fun getCurrentSystemVolume(context: Context): Int {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        return audioManager.getStreamVolume(STREAM_TYPE)
    }

    fun setSystemVolume(context: Context, volume: Int) {
        val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        audioManager.setStreamVolume(STREAM_TYPE, volume, AudioManager.FLAG_SHOW_UI)
    }

    fun saveVolume(context: Context, volume: Int) {
        PrefsUtil.putInt(context, KEY_VOLUME, volume)
    }

    fun getSavedVolume(context: Context): Int {
        return PrefsUtil.getInt(context, KEY_VOLUME, -1)
    }
}